package com.tobby.camelinaction;

import org.apache.camel.DynamicRouter;
import org.apache.camel.Exchange;
import org.apache.camel.Header;

/**
 * Bean which implements the logic where the message should be routed by the Dynamic
 * Router EIP.
 * 
 * @author Administrator
 * @version $Revision$
 */
public class DynamicRouterBean {

	/**
	 * The method invoded by Dynamic Router EIP to compute where to go next
	 * 
	 * @param body the message body
	 * @param previous the previous endpoint, is <tt>null</tt> on the first invocation
	 * 
	 * @return endpoint uri where to go, or <tt>null</tt> to indicate no more
	 */
	@DynamicRouter
	public String route(String body, @Header(Exchange.SLIP_ENDPOINT) String previous) {
		return whereToGo(body, previous);
	}

	// method to compute where to go next
	private String whereToGo(String body, String previous) {
		if (previous == null) {
			// 1st time
			return "mock:a";
		}
		else if ("mock://a".equals(previous)) {
			// 2nd time - translate the message body using the simple language
			return "language://simple:Bye ${body}";
		}
		else {
			// no more, so return null to indicate end of dynamic router
			return null;
		}
	}

}
